package com.nt.train.processor;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.ResourceLoader;
import java.util.Set;

/**
 * ServicePostProcessor实现了BeanDefinitionRegistryPostProcessor接口，
 * 那么在容器启动时候，就会调用实现方法postProcessBeanDefinitionRegistry
 * @author hlf
 * @version 1.0
 * @date 2022/8/11 15:47
 */
public class ServicePostProcessor implements BeanDefinitionRegistryPostProcessor, ResourceLoaderAware {

    private Set<String> packages;

    ResourceLoader resourceLoader;

    public ServicePostProcessor(Set<String> packages) {
        this.packages = packages;
    }

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        // MyBeanScanner ,用来加载被@MyService指定的Bean，其实现在下方
        MyBeanScanner myBeanScanner = new MyBeanScanner(registry);
        myBeanScanner.setResourceLoader(resourceLoader);
        myBeanScanner.registFilters();
        for (String packageValue : packages) {
            myBeanScanner.scan(packageValue);
        }
    }
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}